package com.niuxiaofa.newlaw.Activity;


import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.AreaCodeBean;
import com.niuxiaofa.newlaw.Network.UserPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.View.CommonPopWindow;
import com.niuxiaofa.newlaw.View.MyLinearlayout;
import com.niuxiaofa.newlaw.adapter.AreaAdapter;
import com.niuxiaofa.newlaw.adapter.AutoPollAdapter;
import com.niuxiaofa.newlaw.adapter.AutoPollRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import www.linwg.org.lib.LCardView;


public class LoginActivity extends BaseActivity implements CommonPopWindow.ViewClickListener {

    @BindView(R.id.recyclerview)
    AutoPollRecyclerView recyclerView;

    @BindView(R.id.phone)
    EditText phone;

    @BindView(R.id.getcode)
    LCardView getcode;


    @BindView(R.id.wxlogin)
    ImageView wxlogin;


    @BindView(R.id.go_username_login)
    TextView go_username_login;

    @BindView(R.id.local)
    TextView local;

    @BindView(R.id.my_linear)
    MyLinearlayout my_linear;

    List<AreaCodeBean.DataDTO> areaCodeBeanList =new ArrayList();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {

        UserPresenter.getAreaCode(new UserPresenter.interface_getAreaCode() {
            @Override
            public void getResponse(AreaCodeBean u) {
                areaCodeBeanList.addAll(u.getData());
            }
        });


        setSystemInvadeBlack(false);
        recyclerView.setLayoutManager(
                new LinearLayoutManager(
                        this,
                        //设置LinearLayoutManager.HORIZONTAL  则水平滚动
                        LinearLayoutManager.VERTICAL, false));

        AutoPollAdapter autoPollAdapter = new AutoPollAdapter(getApplicationContext());
        recyclerView.setAdapter(autoPollAdapter);
        //启动滚动
        recyclerView.start();

        getcode.setOnClickListener(e -> {

            if (phone.getText().toString().length() != 11) {
                Toast("请输入正确的手机号");
                return;
            }
            String[][] a = new String[][]{{"phone", phone.getText().toString()}};
            ToActivityUtil.toNextActivity(RegisterActivity.class, a);
            Map map = new HashMap();
            map.put("phone", phone.getText().toString());
            map.put("type", "REGISTER_LOGIN_SMS_CODE");
            UserPresenter.smsCode(map);
        });


        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() != 11) {
                  //  getcode.setEnabled(false);
                    getcode.setCardBackgroundColor(Color.GRAY);
                } else {
                  //  getcode.setEnabled(true);
                    getcode.setCardBackgroundColor(Color.parseColor("#FFF8CF39"));
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        go_username_login.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(UserNameLoginActivity.class);
        });

        local.setOnClickListener(e->{

            CommonPopWindow.newBuilder()
                    .setView(R.layout.pop_area)
                    .setAnimationStyle(R.style.anim_menu_bottombar)
                    .setBackgroundDrawable(new BitmapDrawable())
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(getContext(), 360))
                    .setViewOnClickListener(this)
                    .setBackgroundDarkEnable(true)
                    .setBackgroundAlpha(0.7f)
                    .setBackgroundDrawable(new ColorDrawable(999999))
                    .build(this)
                    .showAsBottom(my_linear);

        });


        wxlogin.setOnClickListener(e -> {


            // send oauth request
//            final SendAuth.Req req = new SendAuth.Req();
//            req.scope = "snsapi_userinfo";
//            req.state = "wechat_sdk_demo_test";
//            Constant.wx_api.sendReq(req);

        });


//        Map map=new HashMap();
//        map.put("userName",username);
//        map.put("password",userpass);
//        map.put("code", SystemUtil.getDeviceBrand());
//        map.put("smsCode",SystemUtil.getSystemModel());
//        map.put("type",   SystemUtil.getSystemVersion());
//        map.put("openid",   SystemUtil.getAppVersionName(UIUtils.getActivity()));
//        map.put("accessToken",   SystemUtil.getAppVersionName(UIUtils.getActivity()));
//        map.put("unionid",   SystemUtil.getAppVersionName(UIUtils.getActivity()));
//        map.put("nickName",   SystemUtil.getAppVersionName(UIUtils.getActivity()));
//        map.put("header",   SystemUtil.getAppVersionName(UIUtils.getActivity()));
//        map.put("deviceLogin",   SystemUtil.getAppVersionName(UIUtils.getActivity()));

    }


    @Override
    public void getChildView(PopupWindow mPopupWindow, View view, int mLayoutResId) {

        RecyclerView recyclerView=view.findViewById(R.id.recyclerview);

        AreaAdapter arrayAdapter=new AreaAdapter(areaCodeBeanList, new AreaAdapter.Onclick() {
            @Override
            public void onclick(int c) {
                local.setText(areaCodeBeanList.get(c).getPrefix());
                mPopupWindow.dismiss();
            }
        });

        RecyclerViewHelp.setRecycler(recyclerView,0).setAdapter(arrayAdapter);

    }
}
